<?php
session_start();
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>在线投票系统</title>
</head>
<body>
<link href="css/bootstrap.css" rel="stylesheet"></link>
<link href="css/layui.css" rel="stylesheet"></link>
<style>
    h1 {
        text-align: center;
        margin-bottom: 10px;
        line-height: 50px;
    }

    img {
        width: 100%;
        margin-bottom: 10px
    }

    .number {
        float: right;
        font-size: 14px;
        font-weight: normal;
    }

    h5 {
        line-height: 150%
    }

    .row {
        margin: 5px 10px
    }

    .row img {
        cursor: pointer
    }

    .uinfo {
        float: right;
        line-height: 80px;
        margin-right: 10px
    }

    .title {
        width: 80%;
        float: left;
        text-align: center;
    }

    .layui-btn {
        margin-top: 20px
    }
</style>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="layer/layer/layer.js"></script>
<?php
include_once 'conn.php';
$sql = "select * from object order by id desc ";
$result = mysqli_query($conn, $sql);
?>
<div style="margin-top: 45px">
    <h1 class="title">在线投票系统</h1>
    <div class="uinfo">
        <a href="singUp.html">注册</a>
        <a href="login.html">登录</a>
        <a href="admin.php">后台管理</a>
        <?php if (isset($_SESSION['logged']) && $_SESSION['logged'] == 1) { ?>
            <a href="logout.php">注销</a>
        <?php } ?>
    </div>
    <div class="clearfix"></div>
</div>
<div class="row">
    <?php
    $i = 1;
    while ($info = mysqli_fetch_array($result)) {
        ?>
        <div class="col-lg-3 col-md-4 col-sm-6 col-xs-6"><img src="<?php echo $info['pic']; ?>"
                                                              onclick="javascript:vote(<?php echo $info['id']; ?>);">
            <h4>
                <?php echo $info['name']; ?>
                <span class="number">
                    当前票数：
                    <span id="v<?php echo $info['id']; ?>">
                        <?php echo $info['vote']; ?>
                    </span>
                </span>
            </h4>
            <h5><?php echo $info['description']; ?></h5>
        </div>
        <?php
        if ($i % 2 == 0) {
            echo '<div class="clearfix visible-xs-block visible-sm-block"></div>';
        }
        if ($i % 3 == 0) {
            echo '<div class="clearfix visible-md-block"></div>';
        }
        if ($i % 4 == 0) {
            echo '<div class="clearfix visible-lg-block"></div>';
        }
        $i++;
    }
    ?>
</div>

<script>
    var layerCode;

    function vote(id) {
        //可以先判断一下当前是否登录，如果未登录，则直接跳转至登录界面
        var hasLogged;
        <?php
        if (isset($_SESSION['logged']) and $_SESSION['logged'] != "") {
            //说明已经登录
            echo "hasLogged = 1;";
        } else {
            echo "hasLogged = 0;";
        }
        ?>
        if (hasLogged == 0) {
            //说明 未登录
            alert("必须登录后才能投票");
            location.href = 'login.html';
        }
        //location.href = 'vote.php?id=' + id;
        var str = "";
        str += '<table style="margin-top: 15px" border="0" align="center">' +
            '    <tr>' +
            '        <td><img src="code.php" style="width: 200px"></td><td><input id="code"></td>' +
            '    </tr>' +
            '    <tr>' +
            '        <td colspan="2" align="center"><button type="button" onclick="goVote(' + id + ')" class="layui-btn layui-btn-lg layui-btn-normal"> 提　　　交 </button></td>' +
            '    </tr>' +
            '</table>'
        layerCode = layer.open({
            type: 1,
            title: "请输入验证码",
            skin: 'layui-layer-rim', //加上边框
            area: ['420px', '240px'], //宽高
            content: str
        });
    }

    function goVote(id) {
        //先判断是否有输入验证码
        /*if(document.getElementById('code').value.trim() == ""){
            alert('验证码必须要输入');
            return;
        }*/
        var code = $("#code").val().trim();
        if (code == "") {
            alert('验证码必须要输入');
            return;
        }
        //location.href = 'vote.php?id=' + id + '&code=' + code;
        //改为ajax方式投票
        ajax(code, id);
    }

    function ajax(code, id) {
        $.ajax({
            url: "postVote.php",
            data: {
                code: code,
                id: id
            },
            type: "post",
            dataType: "json",
            success: function (d) {
                if (d.error == 1) {
                    alert(d.errMsg);
                    layer.close(layerCode);
                } else {
                    layer.close(layerCode);
                    alert('投票成功');
                    //更新当前对象的票数
                    var voteNumber = parseInt($("#v"+id).text()) + 1;
                    $("#v"+id).text(voteNumber);
                }
            },
            error: function () {
                alert("网络错误");
                layer.close(layerCode);
            }
        })
    }
</script>
</body>
</html>